const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    id:"",//订单id
    orderDetaildata:[], //订单详情
    ispwd:false,
    number:"",
    pwd:["","","","","",""],
    no:"",
    price:0,
    userInfo:'',
    type:1,
    isfirstpwd:false,
    isfocus:false,
    payType:'',
    score:1,
    scoretips:"",
    kscore:0,
    userscore:0,
    leaveprice:0,
    exchangeCode:"",
    iscode:false
  },
  onLoad: function (options) {
    if(options.id){
      this.setData({
        no:options.id,
        price:options.price,
        leaveprice:options.price,
        id:options.id
        // payType:(options.paytype==6)?2:'',
        // type:(options.paytype==6)?2:1,
      })
    }
    this.orderDetail()
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  onShow(){
    this.info()
  },
  inputblur(){
    this.setData({
      isfocus:false
    })
  },
  appinfo(){
    Controller.appinfo({key:'key-score-transfer-rate'}).then(res=>{
      if(res.data.baseData){
        if(res.data.baseData.content){
          var score=Number(res.data.baseData.content)
          console.log(Number(this.data.userInfo.score)/score)
          if(Number(this.data.price)>Number(this.data.userInfo.score)/score){
            this.setData({
              scoretips:"(积分不足)",
              
            })
          }
          this.setData({
            score,
            kscore:(score*Number(this.data.price)).toFixed(0),
            userscore:(Number(this.data.userInfo.score)/score).toFixed(2)
          })
        }
      }
    })
  },
  changepay(e){
    if(e.currentTarget.dataset.index==4){
      this.setData({
        type:e.currentTarget.dataset.index
      })
      
    }else{
      this.setData({
        type:e.currentTarget.dataset.index,
        leaveprice:this.data.price
      })
      if(e.currentTarget.dataset.index==3){
        var leaveprice=(Number(this.data.price)-this.data.userscore).toFixed(2)<=0?0:(Number(this.data.price)-this.data.userscore).toFixed(2)
        this.setData({
          leaveprice
        })
      }
    }
    
  },
  info(){
    Controller.info().then(res=>{
      if(res.data.baseData){
        this.setData({
          userInfo:res.data.baseData
        })
        this.appinfo()
      }
    })
  },
  setpwd(){
    wx.navigateTo({
      url: '/pages/mall/setpwd'
    })
  },
  pay(){
    if(app.globalData.isios){
      wx.showToast({
        title:"ios提示不能支付",
        icon:"none"
      })
      return
    }
    if(this.data.type== 4){
      this.setData({
        iscode:true
      })
      // wx.showModal({
      //   title: '兑换码',
      //   editable: true,
      //   success:(res)=> {
      //     if (res.confirm) {
      //       if(res.content==""){
      //         return;
      //       }
      //       this.setData({
      //         exchangeCode:res.content
      //       })

      //       this.createWxPay()



      //     } else if (res.cancel) {
      //       console.log('用户点击取消')
      //     }
      //   }
      // })
    }else{
      if(this.data.type==1 || this.data.type==3){
        if(this.data.userInfo && this.data.userInfo.payPassword){
          this.setData({
            ispwd:true
          })
        }else{
          wx.showToast({
            title:'还没设置支付密码，请设置',
            icon:"none"
          })
          var that=this;
          setTimeout(()=>{
            // that.setpwd()
            that.setData({
              isfirstpwd:true
            })
          },1000)
          
        }
      }else{
        this.createWxPay()
      }
    }
  },
  comfirmcode(){
    this.setData({
      iscode:false
    })
    this.createWxPay()
  },
  cancelcode(){
    this.setData({
      iscode:false
    })
  },
  changepwd(e){
    var number=e.detail.value;
    var pwd=number.split('');
    if(number){
      this.setData({
        isfocus:true
      })
    }
    if(pwd.length>6){
      return
    }
    if(pwd.length<6){
      for(var i=(6-pwd.length);i>0;i--){
        pwd.push("")
      }
    }
    this.setData({
      number,
      pwd
    })
    if(this.data.userInfo && this.data.userInfo.payPassword){

    }else{
      if(number.length>=6){
        this.setPayPassword()
      }
    }
  },
  changecode(e){
    this.setData({
      exchangeCode:e.detail.value
    })
  },
  setPayPassword(){
    Controller.setPayPassword({
      password: this.data.number
    }).then(res => {
      wx.showToast({
        title: '设置成功！',
        icon: 'success',
        duration: 1500
      })
      this.setData({
        isfocus:false
      })
      this.info()
      var that=this;
      setTimeout(()=>{
        that.closepwd()
      },1000)
    });
  },
  closepwd(){
    this.setData({
      ispwd:false,
      isfirstpwd:false,
      isfocus:false,
      number:"",
      pwd:["","","","","",""],
    })
  },
  createWxPay() {
    var that=this;
    var payType=that.data.type==1?5:that.data.type==3?6:that.data.type==4?7:1
    var obj={}
    obj.orderNumber=that.data.no
    obj.payType=payType
    obj.payPassword=that.data.number
    if(payType ==7){
      obj.exchangeCode=that.data.exchangeCode;
    }
    
    Controller.pay(obj).then(res => {
      if( (that.data.type==2 || that.data.type==3) || (that.data.type==4 && that.data.orderDetaildata.exchangeLogisticsPrice >0)){
        let data=res.data.baseData;
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          timeStamp: data.timeStamp,
          signType: data.signType,
          success(res){
            setTimeout(() => {
              wx.redirectTo({
                url: "/pages/Me/order"
              })
            },1000)
            wx.showToast({
              title: "支付成功",
              icon: 'none',
              duration: 1000
            });
          },
          fail(res){
            that.updatePaidScoreOrder()
          }
        })
      }else{
        setTimeout(() => {
          wx.redirectTo({
            url: "/pages/Me/order"
          })
        },1000)
        wx.showToast({
          title: "支付成功",
          icon: 'none',
          duration: 1000
        });
      }
    }).catch(res=>{
      // setTimeout(() => {
      //   wx.redirectTo({
      //     url: "/pages/Me/order"
      //   })
      // },1000)
      // wx.showToast({
      //   title: "支付失败",
      //   icon: 'none',
      //   duration: 1000
      // });
    })
    that.closepwd()
  },
  updatePaidScoreOrder(){
    Controller.updatePaidScoreOrder({
      orderNumber: this.data.no
    }).then(res => {
      setTimeout(() => {
        wx.redirectTo({
          url: "/pages/Me/order"
        })
      },1000)
      wx.showToast({
        title: "支付失败",
        icon: 'none',
        duration: 1000
      });
    }).catch(res=>{
      setTimeout(() => {
        wx.redirectTo({
          url: "/pages/Me/order"
        })
      },1000)
      wx.showToast({
        title: "支付失败",
        icon: 'none',
        duration: 1000
      });
    })
  },
  //订单详情
  orderDetail(){
    var that=this;
    Controller.orderdetail({orderNumber:that.data.id}).then(res=>{
      if(res.data){
        that.setData({
          orderDetaildata:res.data.baseData
        })
      }
    })
  },
})